<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>空明物联网服务平台</title>
    <link rel="stylesheet" href="/static/css/bootstrap.min.css" />
    <link rel="stylesheet" href="/static/css/base.css" />
</head>


<body>
    <div class="container-fluid">
        <h3><span class="glyphicon glyphicon-plus"></span> 添加设备</h3>
        <hr>
        <p>1. 安装服务程序</p>
        <pre id="ininfo"></pre>
        <br>
        <p>2. 填写设备信息</p>
        <div class="form-inline">

            <input class="form-control" style=" width: 300px;" type="text" id="sn" placeholder="设备SN"
                readonly="readonly" name="sn" required /> <button class="btn btn-default" id="custom">自定义</button>
        </div>
        <br />
        <input class="form-control" type="text" id="name" placeholder="设备名称" required />
        <br>
        <button id="btn-add" class="btn btn-primary" type="submit">确定</button>
        <button class="btn btn-default" onClick="javascript:history.go(-1);">返回</button>
    </div>
    <script src="/static/js/jquery-2.1.4.min.js"></script>
    <script src="/static/js/base.js?version=1.0.0"></script>
    <script>
         /** isLoginStats(); **/
        create_sn();

        function create_sn() {
            var uuid = getTokenUUID();
            var ininfo = $('#ininfo').html('curl http://apidata.cc/install.sh | sh -s '+ uuid);
            var sn = $('#sn').val(uuid);
        }

        var sn = $('#custom').click(function () {
            var custom_test = $(this).text()
            if (custom_test == '自定义') {
                var ininfo = $('#ininfo').html('curl http://apidata.cc/install.sh');
                $('#sn').removeAttr("readonly");
                $('#sn').val("");
                $(this).html('生成');
            } else {
                $('#sn').val("");
                $('#sn').attr("readonly", "readonly");
                create_sn();
                $(this).html('自定义');
            }
        });


        function getTokenUUID() {
            var s = [];
            var timestamp = new Date().getTime();
            var hexDigits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
            for (var i = 0; i < 36; i++) {
                s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
            }
            s[14] = "4";
            s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
            s[8] = s[13] = s[18] = s[23];
            // s[0] = timestamp
            var uuid = s.join("");
            return uuid;
        }

        $("#btn-add").click(function () {
            var sn = $("#sn").val();
            var name = $("#name").val();


            if (sn == '') {
                alert("设备SN号不能为空！");
                return false;
            }

            if (name == '') {
                alert("设备名称不能为空！");
                return false;
            }

            new AjaxRequest({
                type: "post",
                url: getServiceURL('deviceadd'),
                param: {
                    "sn": sn,
                    "name": name
                },
                callBack: function (res) {
                    if (res.code == 0) {
                        alert('成功');
                        window.location.reload();
                        return true;
                    }else{
                        alert(res.msg);
                        console.log(res);
                        return false;
                    }

                }
            });
        });
    </script>
</body>

</html>